import QtQuick 1.0
import com.nokia.symbian 1.0
import QtMobility.sensors 1.1
import QtMobility.feedback 1.1

Page {
    id: page5
    anchors.fill: parent
    tools: toolBarMainPage
    orientationLock: PageOrientation.LockPortrait

    property int ballRadius: 25
    property bool gameStarted: false
    property int score: 0

/*
    // NOTE: Using HapticsEffect may cause the application to not
    // exit properly. For details, see:
    // https://bugreports.qt.nokia.com/browse/QTMOBILITY-1464
    // ThemeEffect is not affected by the bug but provides less
    // control.
    HapticsEffect {
        id: feedback
        attackIntensity: 0.0
        attackTime: 250
        intensity: 1.0
        duration: 100
        fadeTime: 250
        fadeIntensity: 0.0
    }
*/

    Rectangle {
        anchors.fill: parent
        color: "black"

        Image {
            id: board
            property int side: Math.min (parent.width, parent.height)
            anchors.horizontalCenter: parent.horizontalCenter
            width: side
            height: side
            source: "qrc:/images/background.png"

            Rectangle { // Game ball
                id: ball
                property int maxX: parent.width - ballRadius * 2
                property int maxY: parent.height - ballRadius * 2
                x: maxX / 2
                y: maxY / 2
                width: ballRadius * 2
                height: ballRadius * 2
                radius: ballRadius
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "black" }
                    GradientStop { position: 1.0; color: "silver" }
                }
                Behavior on x {
                    NumberAnimation {
                        duration: 1400
                        easing.type: Easing.OutBounce
                    }
                }
                Behavior on y {
                    NumberAnimation {
                        duration: 1400
                        easing.type: Easing.OutBounce
                    }
                }
                /*
                onXChanged: {
                    if(x < 1 || x > maxX - 1)
                        feedback.start ();
                }
                onYChanged: {
                    if(y < 1 || y > maxY - 1)
                        feedback.start ();
                }
                */
            }
        }

        Button {
            id: startButton
            anchors.top: board.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Start"
            onClicked: {
                if (gameStarted) {
                    gameStarted = false;
                    startButton.text = "Start";
                    textStatus.visible = true;
                    page5.score = 0;
                    itemTimer.stop();
                }
                else {
                    gameStarted = true;
                    startButton.text = "Stop";
                    textStatus.visible = false;
                    itemTimer.start();
                }
            }
        }

        Text {
            id: textScore
            anchors.top: startButton.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 28
            text: "Score: "
            color: "white"
        }
        Text {
            id: textStatus
            visible: false
            anchors.top: textScore.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 38
            text: "Game Over"
            color: "white"
        }
    }

    RotationSensor {
        active: gameStarted
        onReadingChanged:{
            var readingX = reading.y;
            var readingY = reading.x;
            if (readingX < 0) {
                ball.x = 0;
            }
            else if (readingX > 0) {
                ball.x = ball.maxX;
            }
            if (readingY < 0) {
                ball.y = 0;
            }
            else if (readingY > 0) {
                ball.y = ball.maxY;
            }
        }
    }
    Timer {
        id: itemTimer
        interval: 1000
        repeat: true
        onTriggered: {
            ++parent.score;
            textScore.text = "Score : " + parent.score;
        }
    }
}
